package model.displayer;

import controller.MainController;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import model.Point;
import model.utils.Utils;

public class CircleDisplayer extends ShapeDisplayerJFxImpl
{
    public static final Point DEFAULT_CENTER = new Point(100,100);
    public static final double DEFAULT_RADIUS = 50.0;

    private Circle circle;

    CircleDisplayer(Pane canvas, MainController controller)
    {
        super(canvas,controller);
        this.shape = new Circle();
        shape.setStroke(DEFAULT_STROKE);
        shape.setFill(DEFAULT_FILL);
        shape.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        shape.setOnMouseClicked((MouseEvent e)->controller.handleShapeClicked(this));

        this.circle = (Circle)shape;
        circle.setCenterX(DEFAULT_CENTER.getX());
        circle.setCenterY(DEFAULT_CENTER.getY());
        circle.setRadius(DEFAULT_RADIUS);
    }

    @Override
    protected void resize(double multi)
    {
        double newRadius = circle.getRadius()*(1+multi);
        circle.setRadius(newRadius);
    }

    @Override
    public void setLocation(Point start,Point end)
    {
        Point newCenter = Utils.calcMidPoint(start,end);
        double radius = Utils.calcDistance(start,end)/2;

        circle.setCenterX(newCenter.getX());
        circle.setCenterY(newCenter.getY());
        circle.setRadius(radius);
    }

    @Override
    public ShapeDisplayer clone()
    {
        CircleDisplayer copy = new CircleDisplayer(this.canvas,this.controller);
        copy.circle.setCenterX(this.circle.getCenterX());
        copy.circle.setCenterY(this.circle.getCenterY());
        copy.circle.setRadius(this.circle.getRadius());
        copy.circle.setStroke(this.circle.getStroke());
        copy.circle.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        return copy;
    }

    @Override
    protected Point getCenter(){return new Point(circle.getCenterX(),circle.getCenterY());}

    @Override
    protected void setCenter(Point newCenter)
    {
        circle.setCenterX(newCenter.getX());
        circle.setCenterY(newCenter.getY());
    }

}
